//
// Created by gxx on 2023/11/19.
//

#ifndef TINY_DB_FILE_H
#define TINY_DB_FILE_H

#include "util/base_types.h"

namespace TinyDB {

class TFile {
 public:
  TFile() = default;
  ~TFile() = default;

  TFile(const TFile&) = delete;
  TFile& operator=(const TFile&) = delete;

  virtual t_uint32 Read(t_byte* buf, t_uint32 len, t_uint64 offset) = 0;
  virtual t_void Write(t_byte* buf, t_uint32 len, t_uint64 offset) = 0;
  virtual t_void* Mmap(t_uint32 len, t_uint64 offset) = 0;
  virtual t_void Unmap(t_byte* start, t_uint32 len) = 0;
  virtual t_string GetFileName() = 0;
  virtual t_uint64 GetFileSize();

  virtual t_void Flush() = 0;
  virtual t_void Close() = 0;
};

class TFileUnixImpl final : public TFile {
 public:
  TFileUnixImpl(t_string file_name);
  ~TFileUnixImpl();

  t_uint32 Read(t_byte* buf, t_uint32 len, t_uint64 offset) override;
  t_void Write(t_byte* buf, t_uint32 len, t_uint64 offset) override;
  t_void* Mmap(t_uint32 len, t_uint64 offset) override;
  t_void Unmap(t_byte* start, t_uint32 len) override;
  t_string GetFileName() override;
  t_uint64 GetFileSize() override;

  t_void Flush() override;
  t_void Close() override;

 private:
  t_string file_name_;
  FILE* fp_;
};



}

#endif //TINY_DB_FILE_H
